using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace UtilidadesHera
{
    public partial class Documentacion : Form
    {
        public Documentacion()
        {
            InitializeComponent();
            this.Load += new EventHandler(Documentacion_Load);
            btnAplicar.Click += new EventHandler(btnAplicar_Click);
        }

        void btnAplicar_Click(object sender, EventArgs e)
        {
            string[] busquedas = { "NNNNNN", "dd/mm/aaaa", "dd.mm.aaaa", "MMMMMM", "<NOMEVO>" };
            string[] reemplazos = { mtxtHUNO.Text, mtxtFechaCerti.Text, mtxtFechaCerti.Text, mtxtRMS.Text, txtEvolutivo.Text };
            buscarReemplazar(@"D:\PRUEBAS\NNNNNN-HA-PP-001-1.0.doc", busquedas, reemplazos);
        }

        void Documentacion_Load(object sender, EventArgs e)
        {
            DataTable dtClientes = new DataTable("Clientes");
            dtClientes.Columns.Add("Cod");
            dtClientes.Columns.Add("Desc");
            DataRow drCliente = dtClientes.NewRow();
            drCliente["Cod"] = "0";
            drCliente["Desc"] = "OP";
            dtClientes.Rows.Add(drCliente);
            drCliente = dtClientes.NewRow();
            drCliente["Cod"] = "1";
            drCliente["Desc"] = "PE";
            dtClientes.Rows.Add(drCliente);
            drCliente = dtClientes.NewRow();
            drCliente["Cod"] = "2";
            drCliente["Desc"] = "RNyP";
            dtClientes.Rows.Add(drCliente);

            cmbCliente.DataSource = dtClientes;
            cmbCliente.DisplayMember = dtClientes.Columns["Desc"].ToString();
            cmbCliente.ValueMember = dtClientes.Columns["Cod"].ToString();
            cmbCliente.Refresh();
        }


        private void buscarReemplazar(string ruta, string[] buscar, string[] reemplazar)
        {
            Word.Application AppDoc = new Word.Application();
            Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
            object replaceAll = Word.WdReplace.wdReplaceAll;
            Object oNulo = System.Reflection.Missing.Value;
            object oTrue = true;
            object wrap = Word.WdFindWrap.wdFindContinue;
            object oRuta = ruta;
            
            wordDoc = AppDoc.Documents.Open(
                ref oRuta, ref oNulo, ref oNulo, ref oNulo, ref oNulo,
                ref oNulo, ref oNulo, ref oNulo, ref oNulo, ref oNulo,
                ref oNulo, ref oNulo, ref oNulo, ref oNulo, ref oNulo,
                ref oNulo);

            for (int i = 0; i < buscar.Length; i++)
            {

                AppDoc.Selection.Find.ClearFormatting();
                AppDoc.Selection.Find.Text = buscar[i];

                AppDoc.Selection.Find.Replacement.ClearFormatting();
                AppDoc.Selection.Find.Replacement.Text = reemplazar[i];

                AppDoc.Selection.Find.Execute(ref oNulo, ref oNulo, ref oNulo, ref oNulo, ref oNulo,
                    ref oNulo, ref oNulo, ref wrap, ref oNulo, ref oNulo,
                    ref replaceAll, ref oNulo, ref oNulo, ref oNulo, ref oNulo);
            }
            
            AppDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
            
            for (int i = 0; i < buscar.Length; i++)
            {

                AppDoc.Selection.Find.ClearFormatting();
                AppDoc.Selection.Find.Text = buscar[i];

                AppDoc.Selection.Find.Replacement.ClearFormatting();
                AppDoc.Selection.Find.Replacement.Text = reemplazar[i];

                AppDoc.Selection.Find.Execute(ref oNulo, ref oNulo, ref oNulo, ref oNulo, ref oNulo,
                    ref oNulo, ref oNulo, ref wrap, ref oNulo, ref oNulo,
                    ref replaceAll, ref oNulo, ref oNulo, ref oNulo, ref oNulo);
            }
            wordDoc.Close(ref oTrue, ref oNulo, ref oNulo);
        }
    }
}